﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
 

namespace Sale_Operation
{
    public class BaseDataOp
    {
        /// <summary>
        /// 根据条件获取基础数据信息
        /// </summary>
        /// <param name="strCondition"></param>
        /// <param name="iCurrentPageIndex"></param>
        /// <param name="iRecordCount"></param>
        /// <returns></returns>
        public DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            DataBase db = new DataBase(); 
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT BData_Id,BData_Code,BData_Name,BData_Type,BData_InOrOut,State,CreateTime,CreateMan,[NAME] AS BData_TypeName FROM BaseData LEFT JOIN View_State ON (View_State.CODE= BData_Type )WHERE State=1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY CreateTime DESC");
                db.Data= SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            { 
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public DataTable GetList(string strCondition)
        {
            DataBase db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT BData_Id,BData_Code,BData_Name,BData_Type,BData_InOrOut,State,CreateTime,CreateMan,[NAME] AS BData_TypeName FROM BaseData LEFT JOIN View_State ON (View_State.CODE= BData_Type )WHERE State=1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY CreateTime DESC");

                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
            }
            catch (Exception ex)
            {
            }
            db.Close();
            return db.Data.Tables[0];
        }
        /// <summary>
        /// 获取基础数据类型
        /// </summary>
        /// <returns></returns>
        public DataTable GetTypeList()
        {
            DataBase db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT CODE,[NAME],TYPE FROM View_State WHERE EnabelEdit=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtState"});
            }
            catch (Exception ex)
            {
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public Sale_Model.BaseData GetModel(Sale_Model.BaseData SModel)
        {
            DataBase db = new DataBase(); 
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT BData_Id,BData_Code,BData_Name,BData_Type,BData_InOrOut,State,CreateTime,CreateMan,[NAME] AS BData_TypeName FROM BaseData LEFT JOIN View_State ON (View_State.CODE= BData_Type )WHERE State=1 AND BData_Id=" + SModel.BData_Id);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    SModel.BData_Code = ValueHandler.GetStringValue(db.Data.Tables[0].Rows[0]["BData_Code"]);
                    SModel.BData_Name = ValueHandler.GetStringValue(db.Data.Tables[0].Rows[0]["BData_Name"]);
                    SModel.BData_Type = ValueHandler.GetIntNumberValue(db.Data.Tables[0].Rows[0]["BData_Type"]);
                    SModel.BData_InOrOut = ValueHandler.GetIntNumberValue(db.Data.Tables[0].Rows[0]["BData_InOrOut"]);
                    SModel.CreateMan = ValueHandler.GetIntNumberValue(db.Data.Tables[0].Rows[0]["CreateMan"]);
                    SModel.CreateTime = DateTime.Parse( ValueHandler.GetStringValue(db.Data.Tables[0].Rows[0]["CreateTime"]));
                    SModel.State = ValueHandler.GetIntNumberValue(db.Data.Tables[0].Rows[0]["State"]);
                }
            }
            catch (Exception ex)
            {
            }
            db.Close();
            return SModel;
            
        }
        public bool Save(Sale_Model.BaseData SM)
        {
            DataBase db = new DataBase();
            bool blRes = false;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                if (SM.BData_Id == 0)
                {
                    sbSql.Append("INSERT INTO BaseData (BData_Code,BData_Name,BData_Type,BData_InOrOut,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("BData_Code='" + ValueHandler.GetStringValue(SM.BData_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BData_Name='" + ValueHandler.GetStringValue(SM.BData_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BData_Type='" + ValueHandler.GetStringValue(SM.BData_Type, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BData_InOrOut='" + ValueHandler.GetStringValue(SM.BData_InOrOut, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State='" + ValueHandler.GetStringValue(SM.State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.BData_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "新增基础数据[" + ValueHandler.GetStringValue(SM.BData_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.字典管理);

                }
                else
                {
                    sbSql.Append("UPDATE BaseData SET ");
                    sbSql.Append("BData_Code='" + ValueHandler.GetStringValue(SM.BData_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BData_Name='" + ValueHandler.GetStringValue(SM.BData_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("BData_Type='" + ValueHandler.GetStringValue(SM.BData_Type, ValueHandler.CharHandlerType.IsRepChar) +"',");
                    sbSql.Append("BData_InOrOut='" + ValueHandler.GetStringValue(SM.BData_InOrOut, ValueHandler.CharHandlerType.IsRepChar) + "' ");                    
                    sbSql.Append("WHERE BData_Id="+SM.BData_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "修改基础数据[" + ValueHandler.GetStringValue(SM.BData_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.字典管理);
                }
                blRes = true;

            }
            catch(Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改基础数据出错！["+ex.ToString()+"]", EMenuList.字典管理);
            }
            db.Close();
            return blRes;
        }
        public bool Delete(Sale_Model.BaseData SM)
        {
            DataBase db = new DataBase();
            bool blRes = false;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("UPDATE BaseData SET ");
                sbSql.Append("State=0");
                sbSql.Append("WHERE BData_Id=" + SM.BData_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true; 
                LinkService.InsertLog(ESysLogType.Operation, "删除基础数据[" + ValueHandler.GetStringValue(SM.BData_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.字典管理);
            }
            catch (Exception ex)
            { 
                LinkService.InsertLog(ESysLogType.Error, "删除基础数据出错！[" + ex.ToString() + "]", EMenuList.字典管理);
            }
            db.Close();
           
            return blRes;
        }
        /// <summary>
        /// 校验是否存在,校验编码或者名称是否存在
        /// </summary>
        /// <param name="SM"></param>
        /// <returns></returns>
        public bool HasExists(Sale_Model.BaseData SM)
        {
            DataBase db = new DataBase();
            bool blRes = true;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM BaseData WHERE BData_Code='" + SM.BData_Code + "' AND BData_Id<>" + SM.BData_Id + " AND State=1 AND BData_Type=" + SM.BData_Type);
                sbSql.Append(" UNION ALL ");
                sbSql.Append(" SELECT * FROM BaseData WHERE BData_Name='" + SM.BData_Name + "' AND BData_Id<>" + SM.BData_Id + " AND State=1 AND BData_Type=" + SM.BData_Type);
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList"});
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            { 
            }
            db.Close();
            return blRes;
        }
    }
}
